//
//  WMEClubInformationPhotoTableViewCell.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/3/15.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEClubInformationPhotoTableViewCell.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
#import <AFNetworking.h>
@interface WMEClubInformationPhotoTableViewCell ()<UIScrollViewDelegate>
@property (nonatomic, retain)UIImageView *avatar;
@property (nonatomic, retain)UILabel *senderName;
@property (nonatomic, retain)UIView *commentsView;
@property (nonatomic, retain)UILabel *content;
@property (nonatomic, retain)UIImageView *reply_countImg;
@property (nonatomic, retain)UILabel *reply_count;
@property (nonatomic, retain)UIImageView *like_countImg;
@property (nonatomic, retain)UILabel *like_count;
@property (nonatomic, retain)UIButton *diandiandian;
@property (nonatomic, retain)UILabel *reply_name;
@property (nonatomic, retain)UILabel *gengduo;
@property (nonatomic, retain)UILabel *add_datetime_ts;
@property (nonatomic, retain)UILabel *line;
@property (nonatomic, retain)UIImageView *path;
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, assign)CGRect originalFrame;
@property (nonatomic, retain)UIImageView *lastImageView;

@end
@implementation WMEClubInformationPhotoTableViewCell
- (void)dealloc
{
    [_avatar release];
    [_senderName release];
    [_commentsView release];
    [_content release];
    [_reply_countImg release];
    [_reply_count release];
    [_like_countImg release];
    [_like_count release];
    [_reply_name release];
    [_gengduo release];
    [_add_datetime_ts release];
    [_line release];
    [_path release];
    [_scrollView release];
    [_lastImageView release];
    [super dealloc];
}
#pragma mark -初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //消息的发送者的头像
        self.avatar = [[UIImageView alloc] init];
        //        _avatar.backgroundColor = [UIColor yellowColor];
        _avatar.layer.cornerRadius = 20;
        _avatar.contentMode = UIViewContentModeScaleAspectFill;
        _avatar.clipsToBounds = YES;
        _avatar.userInteractionEnabled = YES;
        //点击跳转 用户界面的手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarTapAction:)];
        [self.avatar addGestureRecognizer:tap];
        [self.contentView addSubview:_avatar];
        [_avatar release];
        [tap release];
        
        //用户名的图片
        self.senderName = [[UILabel alloc] init];
        //        _senderName.backgroundColor = [UIColor redColor];
        _senderName.textColor = [UIColor colorWithRed:0 green:160/255.0 blue:233/255.0 alpha:1];
        _senderName.font = [UIFont boldSystemFontOfSize:11];
        //点击手势跳转 用户界面
        UITapGestureRecognizer *nameTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarTapAction:)];
        _senderName.userInteractionEnabled = YES;
        [self.senderName addGestureRecognizer:nameTap];
        [self.contentView addSubview:_senderName];
        [_senderName release];
        [nameTap release];
        
        //评论视图
        self.commentsView = [[UIView alloc] init];
        _commentsView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_commentsView];
        [_commentsView release];
        
        //cell中的主文本
        self.content = [[UILabel alloc] init];
        //        _content.backgroundColor = [UIColor orangeColor];
        _content.numberOfLines = 0;
        _content.font = [UIFont systemFontOfSize:13];
        [self.commentsView addSubview:_content];
        [_content release];
        
        //点赞的图标
        self.like_countImg = [[UIImageView alloc] init];
        //        _like_countImg.backgroundColor = [UIColor redColor];
        [self.commentsView addSubview:_like_countImg];
        [_like_countImg release];
        
        //点赞的人数
        self.like_count = [[UILabel alloc] init];
        //        _like_count.backgroundColor = [UIColor lightGrayColor];
        _like_count.font = [UIFont systemFontOfSize:11];
        [self.commentsView addSubview:_like_count];
        [_like_count release];
        
        //评论的图标
        self.reply_countImg = [[UIImageView alloc] init];
        //        _reply_countImg.backgroundColor = [UIColor lightGrayColor];
        [self.commentsView addSubview:_reply_countImg];
        [_reply_countImg release];
        
        //评论的人数
        self.reply_count = [[UILabel alloc] init];
        //        _reply_count.backgroundColor = [UIColor blueColor];
        _reply_count.font = [UIFont systemFontOfSize:11];
        [self.commentsView addSubview:_reply_count];
        [_reply_count release];
        
        //3个点
        self.diandiandian = [UIButton buttonWithType:UIButtonTypeSystem];
        //        _diandiandian.backgroundColor = [UIColor grayColor];
        [_diandiandian setImage:[UIImage imageNamed:@"222@2x"] forState:UIControlStateNormal];
        [self.commentsView addSubview:_diandiandian];
        [_diandiandian release];
        
        //评论人的名字
        self.reply_name = [[UILabel alloc] init];
        //        _reply_name.backgroundColor = [UIColor greenColor];
        _reply_name.font = [UIFont systemFontOfSize:11];
        _reply_name.numberOfLines = 0;
        [self.commentsView addSubview:_reply_name];
        [_reply_name release];
        
        //更多
        self.gengduo = [[UILabel alloc] init];
        //        _gengduo.backgroundColor = [UIColor redColor];
        _gengduo.font = [UIFont systemFontOfSize:9];
        [self.commentsView addSubview:_gengduo];
        [_gengduo release];
        
        //美观用的线
        self.line = [[UILabel alloc] init];
        _line.backgroundColor = [UIColor lightGrayColor];
        [self.commentsView addSubview:_line];
        [_line release];
        
        //图片
        self.path = [[UIImageView alloc] init];
        _path.contentMode = UIViewContentModeScaleAspectFill;
        _path.clipsToBounds = YES;
        [self.commentsView addSubview:_path];
        _path.userInteractionEnabled = YES;
        //点击图片的手势
        UITapGestureRecognizer *pathTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pathTapAction:)];
        [_path addGestureRecognizer:pathTap];
        [pathTap release];
        [_path release];
    }
    return self;
}
#pragma mark - 重写setting方法
- (void)setModel:(WMEClubInformationModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
        [_avatar sd_setImageWithURL:[NSURL URLWithString:_model.senderAvatar]];
        
        _senderName.text = _model.senderName;
        
        _content.text = _model.content;
        
        _reply_countImg.image = [UIImage imageNamed:@"pinglun"];
        
        _reply_count.text = _model.reply_count.stringValue;
        
        _like_count.text = _model.like_count.stringValue;
        
        _like_countImg.image = [UIImage imageNamed:@"zan"];
        
        [_path sd_setImageWithURL:[NSURL URLWithString:_model.path]];
        
        if (_model.reply_count.integerValue != 0) {
            
            _reply_name.text = [NSString stringWithFormat:@"%@ : %@", _model.repliesSenderName, _model.repliesContent];
            
            NSInteger i = _model.repliesSenderName.length;
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ : %@", _model.repliesSenderName, _model.repliesContent]];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0 green:160/255.0 blue:233/255.0 alpha:1] range:NSMakeRange(0, i)];
            _reply_name.attributedText = str;
            
            _gengduo.text = @"点击查看更多";
        }
    }
}
#pragma mark -布局函数
-(void)layoutSubviews
{
    [super layoutSubviews];
    __block __typeof(&*self)bs = self;
    _avatar.frame = CGRectMake(10, 0, 40, 40);
    _senderName.frame = CGRectMake(_avatar.frame.size.width + _avatar.frame.origin.x, 0, WIDTH - 100, 20);
    
    CGFloat contentHeight = [[self class] heightWithText:_model.content FontOfSize:13];
    _commentsView.frame = CGRectMake(_avatar.frame.size.width + 20, _senderName.frame.size.height, WIDTH - 70, contentHeight + 60);
    
    _content.frame = CGRectMake(10, 15, WIDTH - 90, contentHeight);
    
    [_path mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 80));
        make.left.equalTo(_commentsView.mas_left).with.offset(20);
        make.top.equalTo(_content.mas_bottom).with.offset(20);
    }];
    
    [_reply_countImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(15, 15));
        make.top.equalTo(_path.mas_bottom).with.offset(20);
        make.left.equalTo(_commentsView.mas_left).with.offset(20);
    }];
    
    [_reply_count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_path.mas_bottom).with.offset(20);
        make.left.equalTo(_reply_countImg.mas_right).with.offset(0);
        make.size.mas_equalTo(CGSizeMake((_commentsView.frame.size.width - 100) / 2, 15));
    }];
    
    
    [_like_countImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(15, 15));
        make.top.equalTo(_path.mas_bottom).with.offset(20);
        make.left.equalTo(_reply_count.mas_right).with.offset(0);
    }];
    
    [_like_count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake((_commentsView.frame.size.width - 100) / 2, 15));
        make.top.equalTo(_path.mas_bottom).with.offset(20);
        make.left.equalTo(_like_countImg.mas_right).with.offset(0);
    }];
    
    [_diandiandian mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(15, 15));
        make.top.equalTo(_path.mas_bottom).with.offset(20);
        make.right.equalTo(bs.mas_right).with.offset(-20);
    }];
    
    if (_model.reply_count.integerValue != 0) {
        
        CGFloat contentHeight = [[self class] heightWithText:_model.content FontOfSize:13];
        _line.frame = CGRectMake(0, contentHeight + 20 + 15 + 10 + 20 +100, WIDTH - 70, 0.4);
        NSLog(@"%@", _line);
        
        CGFloat replyHeight = [[self class] heightWithText:[NSString stringWithFormat:@"%@%@", _model.repliesSenderName, _model.repliesContent] FontOfSize:11];
        _reply_name.frame = CGRectMake(10, _line.frame.size.height + _line.frame.origin.y + 5, WIDTH - 90, replyHeight);
        
        _gengduo.frame = CGRectMake(10, _reply_name.frame.size.height + _reply_name.frame.origin.y + 5, WIDTH - 20, 10);
       
        
        _commentsView.frame = CGRectMake(_avatar.frame.size.width + 20, _senderName.frame.size.height, WIDTH - 70, contentHeight + replyHeight + 100 +100);
        
    }else{
        _line.frame = CGRectMake(0, _like_count.frame.origin.y + _like_count.frame.size.height + 10 +100, 0, 0);
        
        _reply_name.frame = CGRectMake(10, _line.frame.size.height + _line.frame.origin.y, 0, 0);
        
        _gengduo.frame = CGRectMake(10, _reply_name.frame.size.height + _reply_name.frame.origin.y, 0, 0);
        
        _commentsView.frame = CGRectMake(_avatar.frame.size.width + 20, _senderName.frame.size.height, WIDTH - 70, contentHeight + 60 + 100);
    }
    
}
#pragma maek -图片点击
-(void)pathTapAction:(UITapGestureRecognizer *)tap
{
    if (![(UIImageView *)tap.view image]) {
        return;
    }
    //scrollView作为背景
    UIScrollView *bgView = [[UIScrollView alloc] init];
    bgView.frame = [UIScreen mainScreen].bounds;
    bgView.backgroundColor = [UIColor blackColor];
    bgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)];
    [bgView addGestureRecognizer:tapBg];
    
    UIImageView *picView = (UIImageView *)tap.view;
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = picView.image;
    imageView.frame = [bgView convertRect:picView.frame fromView:self];
    [bgView addSubview:imageView];
    
    [[[UIApplication sharedApplication] keyWindow] addSubview:bgView];
    
    self.lastImageView = imageView;
    self.originalFrame = imageView.frame;
    self.scrollView = bgView;
    //最大放大比例
    self.scrollView.maximumZoomScale = 1.5;
    self.scrollView.delegate = self;
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = imageView.frame;
        frame.size.width = bgView.frame.size.width;
        frame.size.height = frame.size.width * (imageView.image.size.height / imageView.image.size.width);
        frame.origin.x = 0;
        frame.origin.y = (bgView.frame.size.height - frame.size.height) * 0.5;
        imageView.frame = frame;
        if (frame.size.height > HEIGHT) {
            _scrollView.contentSize = CGSizeMake(frame.size.width, frame.size.height);
            frame.origin.x = 0;
            frame.origin.y = 0;
            imageView.frame = frame;
        }
        
    }];
    
}

-(void)tapBgView:(UITapGestureRecognizer *)tapBgRecognizer
{
    self.scrollView.contentOffset = CGPointZero;
    [UIView animateWithDuration:0.5 animations:^{
        self.lastImageView.frame = self.originalFrame;
        tapBgRecognizer.view.backgroundColor = [UIColor clearColor];
    } completion:^(BOOL finished) {
        [tapBgRecognizer.view removeFromSuperview];
        self.scrollView = nil;
        self.lastImageView = nil;
    }];
}



//返回可缩放的视图
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _scrollView.subviews.firstObject;
}

#pragma mark - 头像点击
- (void)avatarTapAction:(UITapGestureRecognizer *)tap
{
    NSString *userTemp = @"http://www.duitang.com/napi/people/profile/?__dtac=%257B%2522_r%2522%253A%2520%2522998121%2522%257D&_ts=1452865118.002704&app_code=gandalf&app_version=5.8%20rv%3A149591&device_name=iPhone%205&device_platform=iPhone5%2C2&include_fields=relationship%2Cbind_status%2Cinterests%2Cidentity%2Ccity%2Ccitycode%2Cgender%2Cbirthday%2Crecommend_info%2Cclub_count&locale=zh_CN&platform_name=iPhone%20OS&platform_version=9.2&screen_height=568&screen_width=320&user_id=11200118";
    NSString *userStr = [userTemp stringByReplacingOccurrencesOfString:@"11200118" withString:[_model.senderId stringValue]];
    //    NSLog(@"%@", userStr);
    //AFN网络请求
    AFHTTPSessionManager *userData = [AFHTTPSessionManager manager];
    [userData GET:userStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //        NSLog(@"%@", responseObject);
        NSMutableDictionary *userDic = responseObject[@"data"];
        [self.delegate userAvatarPhotoPushAction:[_model.selfId stringValue] userDic:userDic];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        nil;
    }];
}

#pragma mark - 文本高度计算
//计算文本高
+ (CGFloat)heightWithText:(NSString *)text FontOfSize:(CGFloat )fontOfSize
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
    //最大范围
    CGSize size = CGSizeMake(WIDTH - 100, 1000);
    //计算 文本 需的rectangular
    CGRect r = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回文本 的 高
    return r.size.height;
}

#pragma mark - 计算文本宽
//计算文本宽
+ (CGFloat)widthWithText:(NSString *)text MySize:(CGSize)size MyFontSize:(CGFloat)fontSize
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    //    CGSize size = CGSizeMake(WIDTH - 100, 10);
    CGRect r = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return r.size.width;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
